$('input[name="type"]').on('change', function() {
    var type = $("input[name='type']:checked").val();
    var times = $('#times').val();
    $.ajax({
        url: '/noinfo/add_watch_name_post/',
        type: 'POST',
        dataType: 'json',
        data: { 
            type: type,
        },
        success: function(data) {
            $('#namelist').empty();
            var name_list = data.name_list;
            $.each(name_list, function(index, name) {
                var option = $('<option>').text(name.name);
                $('#namelist').append(option);
            });
        }
    });
    updateOpt(times, type);
})

// 获取初始未收录数据
$.ajax({
    url: '/noinfo/add_watch_get/',
    type: 'GET',
    async: false,
    dataType: 'json',
    success: function(data) {
        var notincludes = data.notincludes;
        $.each(notincludes, function(index, notinclude) {
            if(notinclude.type == '动画') {
                $('#ul-noanime').append($('<li>').append($('<a href="#" class="no-anime no-type">').text(notinclude.name)))
            } else if(notinclude.type == '电影') {
                $('#ul-nomovie').append($('<li>').append($('<a href="#" class="no-movie no-type">').text(notinclude.name)))
            } else if(notinclude.type == '电视剧') {
                $('#ul-noteleplay').append($('<li>').append($('<a href="#" class="no-teleplay no-type">').text(notinclude.name)))
            } else if(notinclude.type == '小说') {
                $('#ul-nonovel').append($('<li>').append($('<a href="#" class="no-novel no-type">').text(notinclude.name)))
            }
        });
    }
});

$('#times').change(function() {
    var type = $("input[name='type']:checked").val();
    var times = $('#times').val();
    updateOpt(times, type);
});

$('#date_watch_start').change(function() {
    var type = $("input[name='type']:checked").val();
    var date_watch_start = $('#date_watch_start').val();
    if(type == '电影' && date_watch_start.length == 10) {
        $('#date_watch_end').val(date_watch_start);
    }
});

// 清除并批量添加option选项
function updateOption(selectId, texts) {
    $('#' + selectId).empty();
    $.each(texts, function(index, text) {
        var option = $('<option>').text(text);
        $('#' + selectId).append(option);
    });
    $('#' + selectId).val(null);
}

function updateOpt(times, type) {
    if(times=='1') {
        if(type=='动画') {
            var option = ['追番', '完结补番', '老番入坑'];
        } else if(type=='电影') {
            var option = ['影院', '网络首发', '院线下映', '老作新看'];
        } else {
            var option = ['追更', '完结爽看', '老作新看'];
        }
    } else {
        var option = ['续作回顾', '系列回看'];
    }
    updateOption('status', option);
};

// 提交数据
$('#submit').click(function() {
    var type = $("input[name='type']:checked").val();
    var name = $("#name").val();
    var times = $("#times").val();
    var date_watch_start = $("#date_watch_start").val();
    var date_watch_end = $("#date_watch_end").val();
    var status = $("#status").val();
    
    // 发送AJAX请求
    $.ajax({
        url: '/noinfo/add_watch_post/',
        type: 'POST',
        dataType: 'json',
        data: { 
            type: type,
            name: name,
            times: times,
            date_watch_start: date_watch_start,
            date_watch_end: date_watch_end,
            status: status,
        },
        success: function(data) {
            console.log(data);
            if(data.code == 'success') {
                alert(name+' 新增完成');
                location.reload();
            } else {
                alert(data.code);
            } 
        },
        error: function(xhr, status, error) {
            alert('ajax执行失败');
          }
        });
})

window.onload = function() {  
    // 点击未收录作品，自动填充作品类型及名称
    $('.notinclude-list').find('a').click(function(event) {
        event.preventDefault(); // 阻止默认跳转行为
        var a_text = $(this).text();
        if($(this).hasClass('no-anime')) {
            $('#input-anime').prop('checked', true);
        } else if($(this).hasClass('no-movie')) {
            $('#input-movie').prop('checked', true);
        } else if($(this).hasClass('no-teleplay')) {
            $('#input-teleplay').prop('checked', true);
        } else if($(this).hasClass('no-novel')) {
            $('#input-novel').prop('checked', true);
        };
        $('#name').val(a_text); 
        var type = $("input[name='type']:checked").val();
        var times = $('#times').val();
        updateOpt(times, type);
    })

        var notinclude_types = $('.notinclude .notinclude-type');
        $.each(notinclude_types, function(index, notinclude_type) {
            console.log(notinclude_type);
            var li_type = $(notinclude_type).find('.ul-type li'); 
            if(li_type.length==0) {
                $(notinclude_type).css('display', 'none');
            }
        });
}